package com.workoutdiary.data;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbManager implements IDbManager {

	public Connection createConnection() {
		try {
			Class.forName("org.h2.Driver");
	        Connection conn = DriverManager.getConnection("jdbc:h2:diary");
//			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//			String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=person.mdb;";
			
//			Connection conn = DriverManager.getConnection(database, "", "");
			return conn;
		} catch (ClassNotFoundException e) {
			throw new IllegalStateException(
					"Could not load JDBC driver", e);
		} catch (SQLException e) {
			throw new IllegalStateException(
					"Could not connect to the database", e);
		}
	}
	
	
	@Override
	public boolean close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				return false;
			}
		}
		return true;
	}

	@Override
	public void startTransaction(Connection conn) {
		if (conn == null) {
			throw new IllegalArgumentException("Requires a valid connection.");
		}

		try {
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			throw new IllegalStateException("Could not start transaction.", e);
		}
	}

	@Override
	public void commitTransaction(Connection conn) {
		if (conn == null) {
			throw new IllegalArgumentException("Requires a valid connection.");
		}

		try {
			conn.commit();
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			throw new IllegalStateException("Could not commit transaction.", e);
		}

	}

	@Override
	public void rollbackTransaction(Connection conn) {
		if (conn == null) {
			throw new IllegalArgumentException("Requires a valid connection.");
		}

		try {
			conn.rollback();
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			throw new IllegalStateException("Could not rollback transaction.",
					e);
		}

	}

	@Override
	public Statement createStatement(Connection conn) {
		try {
			return conn.createStatement();
		} catch (SQLException e) {
			throw new IllegalStateException("Could not create statement.", e);
		}
	}

	@Override
	public boolean close(Statement st) {
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				return false;
			}
		}
		return true;

	}

	@Override
	public PreparedStatement createPreparedStatement(String sql, Connection conn) {
		try {
			return conn.prepareStatement(sql);
		} catch (SQLException e) {
			throw new IllegalStateException("Could not create prepared statement.", e);
		}
	}

	@Override
	public boolean close(PreparedStatement st) {
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				return false;
			}
		}
		return true;
	}

}
